package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("AddressController")
public class AddressController extends CmiEmaController {

	@RequestMapping("/listAddresss")
	public ModelAndView listAddresss(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = addressPaginationService.updatePagination(request, pagination);
		Set<Address> addresss = addressPaginationService.getPaginationAddresss(pagination);

		mav.addObject("addresss", addresss);
		mav.addObject("entityName", "address");
		mav.addObject("pagination", pagination);
		mav.setViewName("address/listAddresss.jsp");
		return mav;
	}

	public String listAddresss() {
		return "redirect:/listAddresss";
	}

	@RequestMapping("/viewAddress")
	public ModelAndView viewAddress(@RequestParam Integer addressId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("address", addressDAO.findAddressByPrimaryKey(addressId));
		mav.setViewName("address/viewAddress.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteAddresss")
	public ModelAndView confirmDeleteAddresss(
			@RequestParam(value = "deleteAddresssIds", required = true) Set<Integer> deleteAddresssIds) {
		ModelAndView mav = new ModelAndView();

		Set<Address> deleteAddresss = addressDAO.findAddresssByIds(deleteAddresssIds);
		Set<Address> referencedAddresss = addressService.loadReferenceAddresss(deleteAddresss);
		boolean hasReferenced;
		if (referencedAddresss.size() == 0) {
			mav.addObject("deleteEntitys", deleteAddresss);
			hasReferenced = false;
		} else {
			deleteAddresss.removeAll(referencedAddresss);
			mav.addObject("referencedEntitys", referencedAddresss);
			mav.addObject("deleteEntitys", deleteAddresss);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Address");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listAddresss");
		return mav;
	}

	@RequestMapping("/deleteAddress")
	public String deleteAddress(@RequestParam Integer addressId) {
		Address address = addressDAO.findAddressByPrimaryKey(addressId);
		addressService.deleteAddress(address);
		return "forward:/listAddresss";
	}

	@RequestMapping("/confirmDeleteAddress")
	public ModelAndView confirmDeleteAddress(@RequestParam Integer addressId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("address", addressDAO.findAddressByPrimaryKey(addressId));

		mav.setViewName("address/deleteAddress.jsp");

		return mav;
	}

	@RequestMapping("/deleteAddresss")
	public String deleteAddresss(
			@RequestParam(value = "deleteAddresssId", required = true) Set<Integer> deleteAddresssId) {
		addressService.deleteAddresss(deleteAddresssId);
		return "forward:/listAddresss";
	}

	@RequestMapping("/editAddress")
	public ModelAndView editAddress(@RequestParam Integer addressId) {

		ModelAndView mav = new ModelAndView();

		Address address = addressDAO.findAddressByPrimaryKey(addressId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("address", address);

		// 把所有的引用address对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(addressService.loadAddressObjectReferencedSet(address));

		// 将编辑页面address添加到mav中
		mav.setViewName("address/editAddress.jsp");
		return mav;
	}

	@RequestMapping("/newAddress")
	public ModelAndView newAddress() {
		ModelAndView mav = new ModelAndView();
		Address address = new Address();

		mav.addObject("address", address);

		// 把所有的引用address对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(addressService.loadAddressObjectReferencedSet(address));

		mav.addObject("newFlag", true);
		mav.setViewName("address/editAddress.jsp");

		return mav;
	}

	@RequestMapping("/saveAddress")
	public ModelAndView saveAddress(@ModelAttribute Address address) {

		ModelAndView mav = new ModelAndView();

		Address parentTemp = address.getParent();
		Address parent = parentTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(addressDAO.findAddressById(parentTemp.getId()));

		Address existingAddress = CmiHibernateProxyUtil
				.deproxy(addressDAO.findAddressByPhysicalPrimaryKey(address.getName(), parent.getId()));

		address.setParent(parent);
		// 新建记录
		if (address.getId() == null) {
			if (address.equals(existingAddress)) {

				mav.addAllObjects(addressService.loadAddressClassReferencedSet(address));
				// mav = editContinue(mav, address);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("address/editAddress.jsp");
			} else {
				addressService.saveAddress(address);
				mav.setViewName("forward:/listAddresss");
			}
		}
		// 编辑记录
		else {
			Address originAddress = CmiHibernateProxyUtil.deproxy(addressDAO.findAddressByPrimaryKey(address.getId()));
			if (!address.equals(originAddress) && address.equals(existingAddress)) {
				mav.addAllObjects(addressService.loadAddressClassReferencedSet(address));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("address/editAddress.jsp");
			} else {
				addressService.saveAddress(address);
				mav.setViewName("forward:/listAddresss");
			}
		}
		return mav;
	}

	@RequestMapping("/addressController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
